package com.gitee.j2cache.service.impl;

import com.gitee.j2cache.entity.User;
import com.gitee.j2cache.mapper.UserMapper;
import com.gitee.j2cache.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * UserServiceImpl
 *
 * @Author: tedking
 * @Date: 2025-02-18
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public boolean save(User user) {
        return userMapper.insert(user) > 0;
    }

    @Override
    @Cacheable(value = {"xj"}, key = "#id")
    public User get(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    @Transactional
    @CachePut(value = {"xj"}, key = "#user.id")
    public boolean update(User user) {
        return userMapper.updateById(user) > 0;
    }

    @Override
    @Transactional
    @CacheEvict(value = {"xj"}, key = "#id")
    public boolean delete(Long id) {
        return userMapper.deleteById(id) > 0;
    }
}
